﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using CarFrameWork.Common;

namespace CarFrameWork.UI.ProductDesign._3DModelingDesign.FenDongQi
{
    public partial class 内挡油板 : PartFormBase
    {
        CarFrameWork.Model.CanShuHua.内挡油板 ndyb = new CarFrameWork.Model.CanShuHua.内挡油板();
        public 内挡油板()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Global.SelectedID = string.Empty;///改动
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (!Panduan())
            {
                return;
            }

            if (ndyb.Exists(Convert.ToInt32(this.cmbIDList.Text.Trim())) == true)
            {
                FormToModel();
                ndyb.ID = Convert.ToInt32(this.cmbIDList.Text.Trim());
                ndyb.Update();
                MessageBox.Show("更新成功", "提示");
                Global.SelectedID = this.cmbIDList.Text.Trim();
                this.Close();
            }
            else
            {
                if (TestContent())
                {
                    FormToModel();
                    ndyb.Add();
                    MessageBox.Show("添加成功", "提示");
                    Global.SelectedID = this.cmbIDList.Text.Trim();
                    this.Close();
                }
                else
                    MessageBox.Show("参数不能为空！", "提示");
                
            }
        }
        private void FormToModel()
        {
            ndyb.ID = 1800031;
            ndyb.name = "内挡油板";
            ndyb.path = "path";
            ndyb.R2 = Convert.ToSingle(textBox1.Text);
            ndyb.R3 = Convert.ToSingle(textBox2.Text);
            ndyb.R4 = Convert.ToSingle(textBox3.Text);
            ndyb.H2 = Convert.ToSingle(textBox4.Text);
            ndyb.D = Convert.ToSingle(textBox5.Text);
       
        }

        private bool Panduan()
        {
            for (int i = 3; i < 8; i++)
            {
                TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                if (float.Parse(textbox.Text) <= 0)
                {
                    MessageBox.Show("对话框中的数值不能为零或负数!");
                    return false;
                }
            }

            if (float.Parse(textBox3.Text) < float.Parse(textBox2.Text))
            {
                MessageBox.Show("对话框中的R4<R3,数值不正确!");
                return false;
            }
            if (float.Parse(textBox1.Text) < float.Parse(textBox3.Text))
            {
                MessageBox.Show("对话框中的R2<R4,数值不正确!");
                return false;
            }

            return true;

        }

        private bool TestContent()
        {
            if (!InspectionText(this.textBox1))
                return false;
            if (!InspectionText(this.textBox2))
                return false;
            if (!InspectionText(this.textBox3))
                return false;
            if (!InspectionText(this.textBox4))
                return false;
            if (!InspectionText(this.textBox5))
                return false;
     
      
            return true;
        }
        private void 内挡油板_Load(object sender, EventArgs e)
        {
            foreach (DataRow row in GetIDList(string.Empty).Tables[0].Rows)
            {
                this.cmbIDList.Items.Add(row[0].ToString().Trim());
            }

            bool tag = false;
            foreach (string str in this.cmbIDList.Items)
            {
                if (str == this.m_ID.ToString())
                {
                    tag = true;
                    break;
                }
            }
            if (!tag)
                this.cmbIDList.Items.Add(this.m_ID.ToString().Trim());
            this.cmbIDList.SelectedIndex = 0;
            this.cmbIDList.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private DataSet GetIDList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID ");
            strSql.Append(" FROM [内挡油板] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private DataSet GetDataList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [内挡油板] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private void cmbIDList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModelToForm(GetDataList("ID = " + cmbIDList.Text.Trim()));
        }

        private void ModelToForm(DataSet dt)
        {
            if (!ndyb.Exists(Convert.ToInt32(cmbIDList.Text.Trim())))
            {
                for (int i = 3; i < 8; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = "";
                }
            }
            else
            {
                for (int i = 3; i < 8; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = dt.Tables[0].Rows[0][i].ToString().Trim();
                }
            }
        }
    }
}